﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using TarreOA.TarreValidationCode;

public partial class admin_DiscussAreaSetting_DiscussAreaIndex : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.DataList_DataTable();
        }
    }
    protected void LbtEdit_Click(object sender, EventArgs e)
    {
        string da_id = ((LinkButton)sender).Attributes["da_id"];
        Response.Redirect("DiscussAreaEdit.aspx?da_id=" + da_id);
    }
    protected void LbtDel_Click(object sender, EventArgs e)
    {
        int da_id = int.Parse(((LinkButton)sender).Attributes["da_id"]);
        DiscussAreaService daService = new DiscussAreaService();
        if (daService.DelDiscussAreaInfoById(da_id) == 1)
        {
            this.DataList_DataTable();
        }
    }
    protected void LbtEditP_Click(object sender, EventArgs e)
    {
        string da_id = ((LinkButton)sender).Attributes["da_id"];
        Response.Redirect("DiscussSettingIndex.aspx?da_id=" + da_id);
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string daid = ((HtmlInputHidden)e.Item.FindControl("HidDaId")).Value;
            ((LinkButton)e.Item.FindControl("LbtDel")).Attributes.Add("onclick", "return confirm('您确认删除此分区吗？')");
            ((LinkButton)e.Item.FindControl("LbtDel")).Attributes.Add("da_id", daid);
            ((LinkButton)e.Item.FindControl("LbtEdit")).Attributes.Add("da_id", daid);
            ((LinkButton) e.Item.FindControl("LbtEditP")).Attributes.Add("da_id", daid);
        }
    }

    private void DataList_DataTable()
    {
        DiscussAreaService daService = new DiscussAreaService();
        DataTable dt = new DataTable();
        dt.Columns.Add("DiscussAreaId", typeof(string));
        dt.Columns.Add("DiscussAreaName", typeof(string));
        dt.Columns.Add("DiscussAreaCreateTime", typeof(string));
        List<DiscussArea> list = daService.findALL();
        if (list != null && list.Count > 0)
        {
            foreach (DiscussArea da in list)
            {
                DataRow dr = dt.NewRow();
                dr["DiscussAreaId"] = da.DiscussAreaId.ToString();
                dr["DiscussAreaName"] = da.DiscussAreaTitle;
                dr["DiscussAreaCreateTime"] = da.DiscussAreaCreateTime.ToString();
                dt.Rows.Add(dr);
            }
        }
        if (dt.Rows.Count > 0)
        {
            this.DataList1.DataSource = dt.DefaultView;
        }
        else
        {
            this.DataList1.DataSource = null;
        }
        this.DataList1.DataBind();
    }

    protected void SengBtn_Click(object sender, EventArgs e)
    {
        string validatorCode = Request.Form.Get(this.TxtValidatorCode.ClientID);
        if (!validatorCode.Equals(Code.Codes))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码输入错误！');", true);
        }
        else
        {
            if (!"".Equals(Request.Form.Get(this.TxtAreaName.ClientID).Trim()))
            {
                DiscussAreaService daService = new DiscussAreaService();
                string areaName = Request.Form.Get(this.TxtAreaName.ClientID);
                if (daService.SaveDiscussAreaInfo(areaName, DateTime.Now) == 1)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('分区创建成功！');", true);
                    this.DataList_DataTable();
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('分区创建失败！');", true);
                }
            }
        }
    }
}
